Создание форматера поля
Модуль форматирования поля форматирует данные поля для просмотра конечным пользователем. Форматировщики полей определяются как плагины, поэтому рекомендуется ознакомиться с API плагинов перед тем, как приступить к написанию нового форматировщика полей.
Класс полевого форматера
Файл: /modules/random/src/Plugin/Field/FieldFormatter/RandomDefaultFormatter.php
<?php
namespace Drupal\random\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Plugin implementation of the 'Random_default' formatter.
*
* @FieldFormatter(
* id = "Random_default",
* label = @Translation("Random text"),
* field_types = {
* "Random"
* }
* )
*/
class RandomDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->t('Displays the random string.');
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
// Render each element as markup.
$element[$delta] = ['#markup' => $item->value];
}
return $element;
}
}
Настройки форматера
Если вашему форматирующему устройству требуются пользовательские настройки отображения, для этого необходимо выполнить три шага:
- Переопределите PluginSettingsBase::defaultSettings(), чтобы установить значения по умолчанию
- Создайте схему конфигурации для созданных вами настроек
- Создайте форму, чтобы позволить пользователям изменять настройки
Шаг 1: Переопределите PluginSettingsBase::defaultSettings(), чтобы установить значения по умолчанию
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
// Declare a setting named 'text_length', with
// a default value of 'short'
'text_length' => 'short',
] + parent::defaultSettings();
}
Шаг 2. Создайте схему конфигурации для созданных вами настроек
Схема конфигурации идет в следующем файле:
[MODULE ROOT]/config/schema/[MODULE_NAME].schema.yml
В этом файле вы опишите тип (ы) данных настроек, которые вы создали в defaultSettings():
Шаг 1 создал параметр с именем 'text_length', в котором хранится строковое значение. Схема для этого будет выглядеть следующим образом:
field.formatter.settings.[FORMATTER ID]:
type: mapping
label: 'FORMATTER NAME text length'
mapping:
text_length:
type: string
label: 'Text Length'
Шаг 3: Создайте форму, чтобы позволить пользователям изменять настройки
Форма, позволяющая пользователям изменять значения параметров, создается путем переопределения FormatterBase::settingsForm().
Не забудьте добавить пространство имен состояния формы в начало файла php.
use Drupal\Core\Form\FormStateInterface;
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['text_length'] = [
'#title' => $this->t('Text length'),
'#type' => 'select',
'#options' => [
'short' => $this->t('Short'),
'long' => $this->t('Long'),
],
'#default_value' => $this->getSetting('text_length'),
];
return $form;
}
Использование #ajax в формах настроек
Использование #ajax в формах настроек не является простым, так как фрагмент формы, созданный в settingsForm(), находится не в корне формы, а скорее глубоко вложен. В приведенном ниже примере форма имеет две настройки: display_type, который может быть либо «label», либо «entity», и entity_display_mode, который может быть «full» или «teaser». Режим отображения объекта отображается только в том случае, если для display_type установлено значение «entity».
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['display_type'] = [
'#title' => $this->t('Display Type'),
'#type' => 'select',
'#options' => [
'label' => $this->t('Label'),
'entity' => $this->t('Entity'),
],
'#default_value' => $this->getSetting('display_type'),
'#ajax' => [
'wrapper' => 'private_message_thread_member_formatter_settings_wrapper',
'callback' => [$this, 'ajaxCallback'],
],
];
$form['entity_display_mode'] = [
'#prefix' => '<div id="private_message_thread_member_formatter_settings_wrapper">',
'#suffix' => '</div>',
];
// First, retrieve the field name for the current field]
$field_name = $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName();
// Next, set the key for the setting for which a value is to be retrieved
$setting_key = 'display_type';
// Try to retrieve a value from the form state. This will not exist on initial page load
if($value = $form_state->getValue(['fields', $field_name, 'settings_edit_form', 'settings', $setting_key])) {
$display_type = $value;
}
// On initial page load, retrieve the default setting
else {
$display_type = $this->getSetting('display_type');
}
if($display_type == 'entity') {
$form['entity_display_mode']['#type'] = 'select';
$form['entity_display_mode']['#title'] = $this->t('View mode');
$form['entity_display_mode']['#options'] = [
'full' => $this->t('Full'),
'teaser' => $this->t('Teaser'),
];
$form['entity_display_mode']['#default_value'] = $this->getSetting('entity_display_mode');
}
else {
// Force the element to render (so that the AJAX wrapper is rendered) even
// When no value is selected
$form['entity_display_mode']['#markup'] = '';
}
return $form;
}
Затем создайте обратный вызов ajax и верните соответствующий элемент формы:
public function ajaxCallback(array $form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName();
$element_to_return = 'entity_display_mode';
return $form['fields'][$field_name]['plugin']['settings_edit_form']['settings'][$element_to_return];
}
Внедрение зависимостей в полевых форматерах
Использование внедрения зависимостей в форматерах полей требует трех шагов:
- Реализовать интерфейс ContainerFactoryPluginInterface
- Реализация ContainerFactoryPluginInterface::create()
- Переопределить FormatterBase::__construct()
1) Реализовать интерфейс ContainerFactoryPluginInterface
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
class MyFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
2) Реализация ContainerFactoryPluginInterface::create()
Этот пример внедряет сервис entity.manager в форматтер
use Symfony\Component\DependencyInjection\ContainerInterface;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
// Add any services you want to inject here
$container->get('entity.manager')
);
}
3) Переопределить FormatterBase::__construct()
Переопределите __construct() в FormatterBase, обязательно вызовите parent ::__construct(), а затем сохраните службу в свойстве класса
use Drupal\Core\Field\FieldDefinitionInterface;
/**
* The entity manager service
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Construct a MyFormatter object.
*
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* Defines an interface for entity field definitions.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* Entity manager service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityManagerInterface $entityManager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityManager = $entityManager;
}
Теперь вы можете использовать менеджер сущностей в любом месте вашего класса форматирования как $this->entityManager.